<template>
  <div class="banner-carousel">
    <div class="carousel-container" ref="carouselContainer">
      <div class="carousel-slides" :style="slideStyle">
        <div v-for="(slide, index) in slides" :key="index" class="carousel-slide">
          <img :src="slide.imageUrl" :alt="slide.title" class="slide-image">
        </div>
      </div>
      <button class="carousel-control prev" @click="prevSlide">←</button>
      <button class="carousel-control next" @click="nextSlide">→</button>
      <div class="carousel-indicators">
        <button
          v-for="(slide, index) in slides" 
          :key="index" 
          class="carousel-indicator" 
          :class="{ active: index === currentIndex }"
          @click="goToSlide(index)"
        ></button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue';

const props = defineProps({
  slides: {
    type: Array,
    required: true,
    validator: (value) => {
      return value.every(slide => slide && slide.imageUrl && slide.title);
    }
  },
  interval: {
    type: Number,
    default: 5000
  }
});

const currentIndex = ref(0);
const carouselContainer = ref(null);
const slideStyle = computed(() => ({
  transform: `translateX(-${currentIndex.value * 100}%)`,
  transition: 'transform 0.5s ease-in-out'
}));
let autoplayTimer = null;

const nextSlide = () => {
  currentIndex.value = (currentIndex.value + 1) % props.slides.length;
};

const prevSlide = () => {
  currentIndex.value = (currentIndex.value - 1 + props.slides.length) % props.slides.length;
};

const goToSlide = (index) => {
  currentIndex.value = index;
};

const startAutoplay = () => {
  autoplayTimer = setInterval(nextSlide, props.interval);
};

const stopAutoplay = () => {
  if (autoplayTimer) {
    clearInterval(autoplayTimer);
    autoplayTimer = null;
  }
};

onMounted(() => {
  startAutoplay();
  
  // Pause autoplay on mouse hover
  if (carouselContainer.value) {
    carouselContainer.value.addEventListener('mouseenter', stopAutoplay);
    carouselContainer.value.addEventListener('mouseleave', startAutoplay);
  }
});

onUnmounted(() => {
  stopAutoplay();
  
  if (carouselContainer.value) {
    carouselContainer.value.removeEventListener('mouseenter', stopAutoplay);
    carouselContainer.value.removeEventListener('mouseleave', startAutoplay);
  }
});
</script>

<style scoped>
.carousel {
  margin: 20px auto;
  max-width: 1400px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.carousel-item {
  height: 400px;
  position: relative;
}

.carousel-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-caption {
  position: absolute;
  bottom: 20px;
  left: 20px;
  right: 20px;
  background: rgba(0, 0, 0, 0.6);
  padding: 16px;
  border-radius: 8px;
}

.carousel-caption h3 {
  color: white;
  font-size: 24px;
  margin: 0;
}

.carousel-caption p {
  color: rgba(255, 255, 255, 0.8);
  font-size: 16px;
  margin: 8px 0 0;
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.5);
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: white;
  font-size: 24px;
  cursor: pointer;
  transition: background 0.2s ease;
}

.carousel-control.prev {
  left: 20px;
}

.carousel-control.next {
  right: 20px;
}

.carousel-control:hover {
  background: rgba(0, 0, 0, 0.8);
}

.carousel-indicators {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.carousel-indicator {
  width: 12px;
  height: 12px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
  cursor: pointer;
}

.carousel-indicator.active {
  background: white;
}
.banner-carousel {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
  border-radius: 16px;
  margin-bottom: 30px;
}

.carousel-container {
  width: 100%;
  height: 100%;
  position: relative;
}

.carousel-slides {
  display: flex;
  height: 100%;
}

.carousel-slide {
  min-width: 100%;
  height: 100%;
}

.slide-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.3);
  color: white;
  border: none;
  width: 40px;
  height: 40px;
  font-size: 20px;
  cursor: pointer;
  transition: background-color 0.2s;
  z-index: 10;
}

.carousel-control.prev {
  left: 10px;
}

.carousel-control.next {
  right: 10px;
}

.carousel-control:hover {
  background-color: rgba(0, 0, 0, 0.5);
}

.carousel-indicators {
  position: absolute;
  bottom: 15px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 10;
}

.carousel-indicators button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: none;
  background-color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: all 0.2s;
}
</style>

.carousel-indicators button.active {
  background-color: white;
  transform: scale(1.2);
}